package com.agricultural.machine.environment;

/**
 * 环境数据类 - 存储环境监测数据
 */
public class EnvironmentData {
    // 土壤数据
    private double soilMoisture;  // 土壤湿度 (%)
    private double soilPH;        // 土壤pH值
    private String soilNutrients; // 土壤养分状态
    
    // 气象数据
    private double temperature;   // 温度 (℃)
    private double humidity;      // 空气湿度 (%)
    private double windSpeed;     // 风速 (m/s)
    
    // 作物状态
    private String cropStatus;    // 作物生长状态
    
    // 障碍物信息
    private boolean obstacleDetected;
    private String obstacleType;
    
    // 环境评估结果
    private boolean isSuitableForSeeding = true;
    private boolean isSuitableForSpraying = true;
    private boolean isSuitableForHarvesting = true;
    
    public EnvironmentData() {
        // 默认值
        this.soilMoisture = 50.0;
        this.soilPH = 7.0;
        this.soilNutrients = "一般";
        this.temperature = 25.0;
        this.humidity = 60.0;
        this.windSpeed = 3.0;
        this.cropStatus = "良好";
        this.obstacleDetected = false;
        this.obstacleType = "";
    }
    
    /**
     * 基于当前环境数据进行评估
     */
    public void evaluateConditions() {
        // 评估播种条件
        if (soilMoisture > 75) {
            isSuitableForSeeding = false; // 土壤过湿不适合播种
        } else if (soilMoisture < 30) {
            isSuitableForSeeding = false; // 土壤过干不适合播种
        } else {
            isSuitableForSeeding = true;
        }
        
        // 评估喷洒条件
        if (windSpeed > 8.0) {
            isSuitableForSpraying = false; // 风速过大不适合喷洒
        } else if (humidity < 40) {
            isSuitableForSpraying = false; // 湿度过低不适合喷洒
        } else {
            isSuitableForSpraying = true;
        }
        
        // 评估收割条件
        if (soilMoisture > 70) {
            isSuitableForHarvesting = false; // 土壤过湿不适合收割
        } else if ("未成熟".equals(cropStatus)) {
            isSuitableForHarvesting = false; // 作物未成熟不适合收割
        } else {
            isSuitableForHarvesting = true;
        }
    }
    
    /**
     * 确定最佳路径策略
     */
    public String determineBestPathStrategy() {
        String bestStrategy;
        String reason;
        
        if (obstacleDetected) {
            bestStrategy = "avoidObstacle";
            reason = "检测到障碍物 (" + obstacleType + ")";
        } else if (soilMoisture > 65) {
            bestStrategy = "terrainOptimized";
            reason = "土壤湿度过高 (" + String.format("%.1f", soilMoisture) + "%)";
        } else {
            bestStrategy = "direct";
            reason = "环境条件良好";
        }
        
        System.out.println("环境评估 => 推荐路径策略: [" + bestStrategy + "], 原因: " + reason);
        return bestStrategy;
    }
    
    // Getters and Setters
    public double getSoilMoisture() {
        return soilMoisture;
    }
    
    public void setSoilMoisture(double soilMoisture) {
        this.soilMoisture = soilMoisture;
        evaluateConditions(); // 重新评估条件
    }
    
    public double getSoilPH() {
        return soilPH;
    }
    
    public void setSoilPH(double soilPH) {
        this.soilPH = soilPH;
    }
    
    public String getSoilNutrients() {
        return soilNutrients;
    }
    
    public void setSoilNutrients(String soilNutrients) {
        this.soilNutrients = soilNutrients;
    }
    
    public double getTemperature() {
        return temperature;
    }
    
    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }
    
    public double getHumidity() {
        return humidity;
    }
    
    public void setHumidity(double humidity) {
        this.humidity = humidity;
        evaluateConditions(); // 重新评估条件
    }
    
    public double getWindSpeed() {
        return windSpeed;
    }
    
    public void setWindSpeed(double windSpeed) {
        this.windSpeed = windSpeed;
        evaluateConditions(); // 重新评估条件
    }
    
    public String getCropStatus() {
        return cropStatus;
    }
    
    public void setCropStatus(String cropStatus) {
        this.cropStatus = cropStatus;
        evaluateConditions(); // 重新评估条件
    }
    
    public boolean isObstacleDetected() {
        return obstacleDetected;
    }
    
    public void setObstacleDetected(boolean obstacleDetected) {
        this.obstacleDetected = obstacleDetected;
    }
    
    public String getObstacleType() {
        return obstacleType;
    }
    
    public void setObstacleType(String obstacleType) {
        this.obstacleType = obstacleType;
    }
    
    public boolean isSuitableForSeeding() {
        return isSuitableForSeeding;
    }
    
    public boolean isSuitableForSpraying() {
        return isSuitableForSpraying;
    }
    
    public boolean isSuitableForHarvesting() {
        return isSuitableForHarvesting;
    }
    
    @Override
    public String toString() {
        return String.format(
                "环境数据 [土壤湿度=%.1f%%, 土壤pH=%.1f, 土壤养分=%s, 温度=%.1f℃, 湿度=%.1f%%, 风速=%.1fm/s, 作物状态=%s, 障碍物=%s]",
                soilMoisture, soilPH, soilNutrients, temperature, humidity, windSpeed, cropStatus,
                obstacleDetected ? obstacleType : "无");
    }
} 